﻿using Models;
using Models.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace yymbDAL
{
    public class IndexService
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public User Login(string name, string pwd)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                User user = db.User.Where(n => (n.E_mail == name || n.Mobile == name) && n.Password == pwd).SingleOrDefault();
                if (user == null)
                {
                    return null;
                }
                else
                {
                    return user;
                }

            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int Register(User user)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                db.User.Add(user);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据邮件和密码获取用户ID
        /// </summary>
        /// <param name="email"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int SelOneUser(string email, string pwd)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                User user = db.User.Where(n => n.E_mail == email && n.Password == pwd).SingleOrDefault();
                return user.UserID;
            }
        }
        /// <summary>
        /// 初始化信息表(Information)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int InsertInfo(Information info)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                db.Information.Add(info);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 首页最新评论用户（大家正在说）
        /// </summary>
        /// <returns></returns>
        public List<Comment> SelNewComm()
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                return db.Comment.Include("User").Include("Information").OrderByDescending(n => n.Comment_time).Take(10).ToList();

            }
        }
        /// <summary>
        /// 关注度TOP10
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IGrouping<string, User_rs>> selTopFollow()
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var data = db.User_rs.Include("Information")
                    .GroupBy(n => n.Information1.Nickname)
                    .OrderByDescending(n => n.Count()).Take(10).ToList();
                return data;

            }
        }
        /// <summary>
        /// 新用户
        /// </summary>
        /// <returns></returns>
        public List<Information> SelNewUser()
        {

            using (yymbDBEntities db = new yymbDBEntities())
            {
                return db.Information.Include("User").OrderByDescending(n => n.User.Creation)
                    .Take(20).ToList();

            }
        }
        /// <summary>
        /// 显示4条点赞数最高的微博
        /// </summary>
        /// <returns></returns>
        public List<Microblog> SelNewMBlogs()
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                return db.Microblog.Include("WB_pic").Include("Information").Include("Comment").Include("Likes").OrderByDescending(n => n.Likes.Count()).Take(4).ToList();
            }
        }
        /// <summary>
        /// 显示点赞数最高的微博
        /// </summary>
        /// <returns></returns>
        public List<Microblog> GetMBlogs()
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                return db.Microblog.Include("WB_pic").Include("Information").Include("Comment").Include("Likes").OrderByDescending(n => n.Likes.Count()).ToList();
            }
        }
        /// <summary>
        /// 获取所有微博的数量
        /// </summary>
        /// <returns></returns>
        public int SumWB()
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                return db.Microblog.ToList().Count();
            }
        }
        /// <summary>
        /// 通过微博id查询这条微博的所有评论
        /// </summary>
        /// <param name="MbID"></param>
        /// <returns></returns>
        public List<Comment> GetComm(int MbID)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                return db.Comment.Include("Information").Where(n => n.MbID == MbID).OrderByDescending(n=>n.Comment_time).ToList();
            }
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public int AddComment(Comment comment)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                db.Comment.Add(comment);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 删除成功
        /// </summary>
        /// <param name="commid"></param>
        /// <returns></returns>
        public int DelComment(int commid)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                Comment comment = db.Comment.Find(commid);
                comment.Comments = "该评论已被删除";
                db.Entry(comment).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges();                       
            }
        }
        /// <summary>
        /// 转发微博
        /// </summary>
        /// <param name="mbid"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public int AddFwmb(Microblog micr)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
                db.Microblog.Add(micr);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据转发的ID查询被转发的那条微博信息
        /// </summary>
        /// <param name="fwID"></param>
        /// <returns></returns>
        public Microblog FwMic(int? fwID)
        {
            using (yymbDBEntities db=new yymbDBEntities())
            {
               return db.Microblog.Include("Information").SingleOrDefault(f => f.MbID == fwID);
            }
        }
    }
}
